package com.justgame.project.service.api.entity;

import com.justgame.project.cloud.common.constant.Const;
import com.justgame.project.cloud.common.constant.Result;
import com.justgame.project.cloud.common.util.*;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;

/**
 * 目标数据源 : MongoDB
 */

@Data
@AllArgsConstructor
@NoArgsConstructor
public class SysLog implements Serializable {
    private final static long serialVersionUID = Const.V;
    private String id;
    private String userId;
    private String ip;
    private String address;
    private String time;
    private String platform;
    private Long costTime;
    private String api;
    private Integer success;
    private Integer responseCode;
    private String responseDataClassType;

    public SysLog(Result<?> result, HttpServletRequest request) {
        this.id = IdUtils.original();
        this.userId = ShiroUtils.getUserId(request);
        this.ip = HttpContextUtil.getIp(request);
        this.address = HttpContextUtil.getRealAddr(request);
        this.time = TimeUtil.simpleDateNow();
        this.costTime = result.getCostTime();
        this.platform = AgentUtils.getPlatform(request);
        this.api = request.getRequestURI();
        this.success = result.isSuccess() ? 1 : 0;
        this.responseCode = result.getCode();
        if (result.getData() == null) {
            this.responseDataClassType = null;
        } else {
            this.responseDataClassType = result.getData().getClass().getTypeName();
        }
    }
}
